package com.techsoft.poolsmgr;

import oracle.ucp.UniversalConnectionPoolException;
import oracle.ucp.admin.UniversalConnectionPoolManager;
import oracle.ucp.admin.UniversalConnectionPoolManagerImpl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.techsoft.config.Configure;
import com.techsoft.interfaces.IPoolService;

public class ConnectionPoolsMgr {

	private static UniversalConnectionPoolManager poolmgr = null;

	public static UniversalConnectionPoolManager getPoolmgr() {
		return poolmgr;
	}

	private static Log log = LogFactory.getLog(ConnectionPoolsMgr.class);

	static {
		try {
			poolmgr = UniversalConnectionPoolManagerImpl
					.getUniversalConnectionPoolManager();
		} catch (UniversalConnectionPoolException e) {
			log.error(e);
		}
	}

	/**
	 * @param 数据库驱动名称
	 * @param 数据库的连接参数
	 * @param 客户的唯一标识
	 *            ，可能是客户的ID，代码或者名称
	 * @param 数据库的账号名称
	 * @param 数据库账号密码
	 * @param 数据库最小连接数
	 * @param 数据库最大连接数
	 * @param 数据库初始化连接数
	 * @return 返回一个连接池接口
	 * @throws Exception
	 */
	public static IPoolService newConnectionPool(String dbsmtype, String url,
			String customername, String username, String password, int minPool,
			int maxPool, int initPool) throws Exception {
		String drivername = Configure.getValue(dbsmtype);
		return new PoolService(drivername, url, customername, username,
				password, minPool, maxPool, initPool);
	}

	/**
	 * @param 客户的唯一标识
	 * @param 最大连接数目
	 * @throws Exception
	 */
	public static void changeMaxPoolCount(String customername, int count)
			throws Exception {
		poolmgr.getConnectionPool(customername).setMaxPoolSize(count);
	}

	/**
	 * @param 客户的唯一标识
	 * @param 最小连接数目
	 * @throws Exception
	 */
	public static void changeMinPoolCount(String customername, int count)
			throws Exception {
		poolmgr.getConnectionPool(customername).setMinPoolSize(count);
	}

	/**
	 * @param 客户的唯一标识
	 * @throws Exception
	 */
	public static void closePool(String customername) throws Exception {
		poolmgr.stopConnectionPool(customername);
	}

	/**
	 * @param 客户的唯一标识
	 * @throws Exception
	 */
	public static void destroyPool(String customername) throws Exception {
		poolmgr.destroyConnectionPool(customername);
	}

	/**
	 * @param 客户的唯一标识
	 * @throws Exception
	 */
	public static void refreshPool(String customername) throws Exception {
		poolmgr.refreshConnectionPool(customername);
	}

	/**
	 * @param 客户的唯一标识
	 * @throws Exception
	 */
	public static void RecyclePool(String customername) throws Exception {
		poolmgr.recycleConnectionPool(customername);
	}

	/**
	 * @param 客户的唯一标识
	 * @throws Exception
	 */
	public static void purgePool(String customername) throws Exception {
		poolmgr.purgeConnectionPool(customername);
	}
}
